﻿using System.Collections.Generic;

namespace Azure.Online.Proxy.Server {
    public class HttpServerMessage {

        private readonly HttpHeadersDictionary _headers = new HttpHeadersDictionary();

        public string ContentType {
            get { return _headers["Content-Type"]; }
            set { _headers["Content-Type"] = value; }
        }

        public long? ContentLength {
            get {
                var value = _headers["Content-Length"];
                if (value == null) return -1;
                long val;
                if (long.TryParse(value, out val)) return val;
                return null;
            }
            set {
                if (value.HasValue) {
                    _headers["Content-Length"] = value.Value.ToString();
                } else {
                    _headers.Remove("Content-Length");
                }
            }
        }

        public bool SendChunked {
            get { return Headers["Transfer-Encoding"] == "chunked"; }
            set {
                if (!value) {
                    Headers.Remove("Transfer-Encoding");
                } else {
                    Headers["Transfer-Encoding"] = "chunked";
                }
            }
        }

        public IDictionary<string, string> Headers {
            get { return _headers; }
        }

    }
}
